Two integers a and
b are given. Print the squares and cubes of all integers from a to
b inclusive.
Input. Two integers a and
b (0 ≤ a ≤ b ≤ 10000).
Output. In the first line,
print the squares of all integers from a to b
inclusive in ascending order. In the second line, print the cubes of all
integers from a to b inclusive
in descending order.
Sample input 1 |
Sample output 1 |
5 10 |
25 36 49 64 81 100 1000 729 512 343 216 125 |
|
|
Sample input 2 |
Sample output 2 |
120 123 |
14400 14641 14884 15129 1860867 1815848 1771561 1728000 |
loop
Algorithm analysis
Use a for
loop to print the squares and cubes of all integers from a to b as
required by the problem statement.
Since b ≤ 104,
it
follows that b3 ≤ 1012.
Use a 64-bit
integer type.
Algorithm
realization
Read the input data.
scanf("%lld %lld", &a, &b);
Print the
squares of all integers from a to b inclusive in ascending order.
for (i = a; i <= b; i++)
printf("%lld
", i * i);
printf("\n");
Print the cubes
of all integers from a to b inclusive in descending order.
for (i = b; i >= a; i--)
printf("%lld
", i * i * i);
printf("\n");
Java realization
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner con = new Scanner(System.in);
long a = con.nextLong();
long b = con.nextLong();
for(long i = a; i <= b; i++)
System.out.print(i*i + " ");
System.out.println();
for(long i = b; i >= a; i--)
System.out.print(i*i*i + " ");
System.out.println();
con.close();
}
}
Python realization
Read the input data.
a, b = map(int,input().split())
Print the
squares of all integers from a to b inclusive in ascending order.
for i in range(a,b+1):
print(i*i, end = " ")
print()
Print the cubes
of all integers from a to b inclusive in descending order.
for i in range(b,a-1,-1):
print(i*i*i, end = " ")
print()